/**
 * 
 */
///////////////////////Serach Supplier////////////////////////////////
var comp_search_cache = {};
var ctx_search_table = null;
var ctx_pref_supp_table = null;
function searchSupplier(searchObj){
	var searchObj = {
		companyName : null,
		doingBusinessAs : null,
		phone : null,
		country : null,
		state : null,
		city : null
	};
	var config = {
			callback : success_searchSupplier,
			exceptionHandler : exception_searchSupplier,
			timeout : 100000,
			errorHandler : exception_searchSupplier	
	};
	searchObj.companyName = dwr.util.getValue('search_comp_name');
	searchObj.doingBusinessAs = dwr.util.getValue('search_comp_business');
	searchObj.phone = dwr.util.getValue('search_comp_phone');
	searchObj.country = dwr.util.getValue('serach_comp_country');
	searchObj.state = dwr.util.getValue('search_comp_State');
	searchObj.city = dwr.util.getValue('search_comp_city');
	jQuery('#suppsrch_activity_throbber').removeClass('throbberOff').addClass('throbberOn');
	DWR_SearchService.searchSupplier(searchObj,config);
}

function success_searchSupplier(companyList){
	jQuery('#suppsrch_activity_throbber').removeClass('throbberOn').addClass('throbberOff');
	comp_search_cache = companyList.objRespOrError;
	render_jqSearchTable();
	
}

function exception_searchSupplier(companyList){
	comp_search_cache = [];
	
}

function render_jqSearchTable() {
	
	if(ctx_search_table != null){
		ctx_search_table = null;
	}	
	ctx_search_table = jQuery('#ctx_common_comp_list_tbl').dataTable({
		"bProcessing" : true,
		"aaData" : comp_search_cache,
		"bJQueryUI" : true,
		"bSortClasses" : false,
		 "bDestroy":  true,
		
		"aoColumns" : [
		{
			"mDataProp" : "companyId",
			"sClass": "alignCenter",
			"fnRender" : function(obj) {
				return "<input type='radio' name='sres_comprw' id='sres_comp_"+obj.aData.companyId+"' onclick='srescomprdoclicked(this);'>";
			},
			"bUseRendered" : false
		},  {
			"mDataProp" : "companyName",
			"sClass": "alignCenter",
			"fnRender" : function(obj) {
   				if(usrcompany_id == obj.aData.companyId) { return obj.aData.companyName + " <br>(Your Comapny)"; } else { return obj.aData.companyName;}
   			},
			"bUseRendered" : false
		}, {
			"mDataProp" : "companyDomain",
			"sType" : "date",
			"sClass": "alignCenter",
			"fnRender" : function(obj) {return obj.aData.companyDomain;},
			"bUseRendered" : false
		}, {
			"mDataProp" : "city",
			"sClass": "alignCenter",
			"fnRender" : function(obj) { return obj.aData.city;},
			"bUseRendered" : false
		}, {
			"mDataProp" : "state",
			"sClass": "alignCenter",
			"fnRender" : function(obj) { return obj.aData.state;},
			"bUseRendered" : false
		},{
			"mDataProp" : "country",
			"sClass": "alignCenter",
			"fnRender" : function(obj) { return obj.aData.country;},
			"bUseRendered" : false
		},
		
		{
			"mDataProp" : "email",
			"sClass": "alignCenter",
			"fnRender" : function(obj) { return obj.aData.email;},
			"bUseRendered" : false
		}
		
		]
	});
}


///////////////////////////////////////////////////////////////////////////////

////////////////////////Add Supplier/////////////////////////////////////////////////
var comp_preferred_cache = {};

function getPreferredSuplierList(){
	jQuery('#suppdel_activity_throbber').removeClass('throbberOff').addClass('throbberOn');
	var config = {
			callback : success_getPreferredSuplierList,
			exceptionHandler : exception_getPreferredSuplierList,
			timeout : 100000,
			errorHandler : exception_getPreferredSuplierList	
	};
	DWR_Register.getPreferredSuplierList(usrcompany_id,config);
}

function success_getPreferredSuplierList(mySuppliers){
	jQuery('#suppdel_activity_throbber').removeClass('throbberOn').addClass('throbberOff');
	 comp_preferred_cache = mySuppliers.objRespOrError;
	 render_jqPreferredCompTable();
}

function exception_getPreferredSuplierList(){jQuery('#suppdel_activity_throbber').removeClass('throbberOn').addClass('throbberOff');}


function render_jqPreferredCompTable() {
	
	
	ctx_pref_supp_table = jQuery('#ctx_common_preferred_comp_list_tbl').dataTable({
		"bProcessing" : true,
		"aaData" : comp_preferred_cache,
		"bJQueryUI" : true,
		"bSortClasses" : false,
		"bDestroy":  true,
		
		"aoColumns" : [
		       		{
		       			"mDataProp" : "companyId",
		       			"sClass": "alignCenter",
		       			"fnRender" : function(obj) {
		       				return "<input type='radio' name='ssup_comprw' id='ssup_comp_"+obj.aData.companyId+"'>";
		       			},
		       			"bUseRendered" : false
		       		},  {
		       			"mDataProp" : "companyName",
		       			"sClass": "alignCenter",
		       			"bUseRendered" : false
		       		}, {
		       			"mDataProp" : "companyDomain",
		       			"sType" : "date",
		       			"sClass": "alignCenter",
		       			"fnRender" : function(obj) {return obj.aData.companyDomain;},
		       			"bUseRendered" : false
		       		}, {
		       			"mDataProp" : "city",
		       			"sClass": "alignCenter",
		       			"fnRender" : function(obj) { return obj.aData.city;},
		       			"bUseRendered" : false
		       		}, {
		       			"mDataProp" : "state",
		       			"sClass": "alignCenter",
		       			"fnRender" : function(obj) { return obj.aData.state;},
		       			"bUseRendered" : false
		       		},{
		       			"mDataProp" : "country",
		       			"sClass": "alignCenter",
		       			"fnRender" : function(obj) { return obj.aData.country;},
		       			"bUseRendered" : false
		       		},
		       		
		       		{
		       			"mDataProp" : "email",
		       			"sClass": "alignCenter",
		       			"fnRender" : function(obj) { return obj.aData.email;},
		       			"bUseRendered" : false
		       		}
		
		]
	});
}

function srescomprdoclicked(ctrl){

}

function addasprefcompany(){
	var r = jQuery("input[name='sres_comprw']");
	if(r != undefined && r != null && r.length > 0){
		var onechkd = false;
		for(var x in r){
			if(r[x].checked){
				onechkd = true;
				var cid = r[x].id.replace('sres_comp_','');
				if(cid == usrcompany_id){
					showusrmessage('You cannot add your own company as supplier.');
				}else{
					jQuery('#suppsrch_activity_throbber').removeClass('throbberOff').addClass('throbberOn');
					addremovePreferredSuplierList(usrcompany_id,[cid],"ADD");
				}
				break;
			}
		}
	
		
		if(!onechkd){
			showusrmessage('Please select a Company from the Search Results.');
		}
	}else{
		showusrmessage('Please search for Suppliers first.');
	}
}

function addremovePreferredSuplierList(usrcompany_id,supplierList,action){
	
	var config = {
			callback : success_addRemoveSupp,
			exceptionHandler : exception_getPreferredSuplierList,
			timeout : 100000,
			errorHandler : exception_getPreferredSuplierList	
	};
	DWR_Register.addRemovePreferredSuplierList(usrcompany_id,supplierList,action,config);
}

function success_addRemoveSupp(dwrresp){
	jQuery('#suppdel_activity_throbber').removeClass('throbberOn').addClass('throbberOff');
	jQuery('#suppsrch_activity_throbber').removeClass('throbberOn').addClass('throbberOff');
	showusrmessage('Supplier action completed successfully');
	success_getPreferredSuplierList(dwrresp);
}

function delfromprefsupp(){
	
	var r = jQuery("input[name='ssup_comprw']");
	if(r != undefined && r != null && r.length > 0){
		var onechkd = false;
		for(var x in r){
			if(r[x].checked){
				onechkd = true;
				var cid = r[x].id.replace('ssup_comp_','');
				jQuery('#suppdel_activity_throbber').removeClass('throbberOff').addClass('throbberOn');
				addremovePreferredSuplierList(usrcompany_id,[cid],"DELETE");
				break;
			}
		}
		
		if(!onechkd){
			showusrmessage('Please select a Supplier.');
		}
	}else{
		showusrmessage('Please select a Supplier.');
	}
}